The single market. Refusing to sell to other EU citizens is very illegal.
These are almost certainly subsidized tickets, financed with taxpayer money, it is common to restrict the sales to those who pay taxes in the covered area. And I don't see how it could be illegal, it is like forcing Germany to provide health care for the French and vice versa.
That's literally how it works [1]. EU citizens are treated the same as local citizens in every country.
[1]: https://www.schengenvisainfo.com/europe-travel-insurance/ehi...
And healthcare operates by your local health system reimbursing the system in country you were visiting - you need to have European Health Insurance Card on you, from your healthcare provider, which helps route the payments appropriately.
For example, in Paris, some "all zones" tickets are only available to residents (pass Navigo). The Japanese "Japan Rail Pass" is only available to tourists, and the "Interrail" pass is complicated: you have to reside in Europe but you can only use it in your own country for a single round trip.
Edit: looks like I was wrong -> see replies. You can also search for "9-Euro-Ticket übertragbar" (übertragbar = transferable) yourself to get a number of different German sources for that.
Ticket machines can be used without need to identify.
However, contrary to what is said multiple times in this thread, it is NOT transferable. You have to put your name on it and have some form of ID with you.
https://www.rnd.de/politik/9-euro-ticket-ab-wann-erhaeltlich...
Google Translate: https://www-rnd-de.translate.goog/politik/9-euro-ticket-ab-w...
Biggest hurdle will be getting a Germany ticket machine to accept a regular credit card!
But otherwise, isn't discriminating based on nationality for good and services illegal?
Pretty sure it's just Germany being pig-headed about it for some reason. Everywhere else in Europe is perfectly happy to accept a credit card. I do 99.999% of all my spending on a credit card, like most millennial people or younger, and the only people in the developed world who this is a problem for is the Germans.
Why not at least google some statistics before you lose yourself in some pig-headed chauvinistic rant about what in the most charitable interpretation amounts to nothing at all.
Paying with a credit card in shops? That's just not done excepting American tourists, same as in a number of European countries. It's contactless debit cards mostly. Building a credit rating by using a credit card is not part of the system.
The EU has imposed caps on these interchange fees [1], which is one reason Europeans are not inundated by junk mail offering credit cards, and may be a reason that credit cards are not ubiquitous. Having said that, I find that I can pay electronically in most places, including Deutsche Bahn ticket machines (or just buy them in the app).
[1] https://en.wikipedia.org/wiki/Interchange_fee#European_Union
That pig-headed reason being that Germany went trough two different repressive regime during the last century.
Regimes that made plenty of use of large scale surveillance and data collections [0] to find a lot of their victims.
That's why cash still reigns supreme in Germany; It's anonymous and third parties can't just remotely disable it by disabling your bank account.
It's also the reason why Germans value their privacy, at least used to.
That's a restriction that wouldn't violate any EU law.