Reduce multiclass classification targets to binary classification targets in scikit-learn
Of the three stated purposes of pipelines, you'd get the &quot;convenience and encapsulation&quot; one, but not the others:

Joint parameter selection: you don't have any parameters for this transformation.
Safety (from data leak): your transformation is context-specific, so there is no data leakage in applying it to the entire dataset up front.

This feels like something that is the definition of the targets, and is best considered a part of the data retrieval.

sklearn expects transform methods to have input just X and not y.  For the most part, you can work around that by overriding fit_transform from TransformerMixin.  However, nothing downstream will expect to get two return values (transformed X and y), so this won't work.
You can make a little more headway with the imbalanced-learn package, which provides its own Pipeline with more flexible transformation syntax.  The purpose there is to implement resamplers, and that throws a major issue: resamplers do not apply at prediction time.