The reasons for the searches you have seen seems to be the fight against street-level drug trading (German news source). Occasionally there are also stronger-than-usual restrictions on the carrying of weapons, which would allow searches for pepper spray, knives, etc., but in the past those were temporary (German official declaration).
The police can question suspects of drug offenses, including searches. Then they either drop their suspicion or take further steps -- the questioning is not, technically, a detention (vorläufige Festnahme), but those details might get lost in translation.
Some activists accuse the police in Frankfurt (federal police, not state police, in the train station) of racial profiling. I'm not qualified to judge how common that is.
In response to a comment made by Joe, about how to avoid being profiled: As someone who is not affected by racial profiling in Germany, I have no personal experiences to share, but I would suggest not hanging around at train stations without actually going somewhere, without luggage. People with suitcases who either come from a platform and leave the station or enter the station to go to a platform are less suspicious. Time of day also matters, but travelers don't usually have control over that.
That won't always help against cops who believe that the drug market is cornered by a specific ethnic group in their town ...