Theoretically, this is covered by Pfandrecht, in which the merchant takes the role of the creditor (Pfandgläubiger / Sicherungsnehmer) and you take the role of the debitor (Sicherungsgeber). The deposit (Pfand / Sicherheit) is your property but remains in the merchant's possession as long as you possess the loan (= the cup) under the premise that you will hopefully return the loan at some point.
Now if you do not return the cup, one may have the opinion that this would strictly be (minor) theft since you attempt to (or actually do) take a moveable object that is owned by another person with the intent of taking over ownership illegitimately. That's arguably, within limits, a kinda legitimate point of view. I wouldn't know if that stands in court in presence of Pfand, though.
However, the very intent of the deposit is (BGB §1204) to satisfy the demand of the creditor in case the debitor does not fulfill (i.e. give back the cup). So, very very very strictly, not returning the cup wouldn't count as theft, but would cause Pfandreife. Which means that the merchant will further on be allowed to spend the money you gave them. End of story. That's the reason why every halfway intelligent merchant (or bank) will only ever give a loan that is worth less than the security.
Practically, everybody does it ever now and then, and nobody cares. Taking home your cup is a perfectly "normal" thing, people won't even raise an eyebrow. Contrarily, you can even ask shop runner for a clean cup that you wish to take home and 99.9% certain they'll happily give you one.
In which case BGB §1205 (2) would apply, the deposit being explicitly, and officially transferred and the loan (the cup) being explicitly forfeitet.
Even more practically, even if the merchant -- hypothetically -- humiliated himself insofar as to call police over the cup (for alleged theft) they would not even come, let alone arrest you (even if the theft claim was considered valid). Law enforcement, in reality, is a sad joke in Germany. There's a law against everything, which probably includes breathing in public places.
But in practice, if you have a permanent residence and unless you already have 25 felonies on your record, there's no way you go to prison, let alone for a cup that's worth 30 cents (and for which you paid 3€). Last week, an 48 year old in Siegburg walked free (well, parole, but that's walked free) after being caught distributing child pornography. If you wonder how that could possibly happen, well, he has a permanent residence, first conviction, and he feels honestly sorry.
So, no worries about your little souvenir cup. You're not going to get a life sentence over taking it.
Possess seems like a funny word, but this is intentional. German common language distinguishes more clearly than English between owning
and being in control of
(= possessing). While you possess
the cup, the merchant nevertheless owns