This sort of arrangement is quite common worldwide. Sometimes the logic is that public transit, both its construction and operating costs, is subsidized by the government, and there's generally more willingness to subsidize transit for local residents than travelers. Airport service requires infrastructure that's dedicated to the airport, and governments often believe that should be paid for specifically by travelers rather than the general public.
And economically, air travelers generally have a much higher willingness to pay than most transit travelers. Someone who has already bought plane tickets is probably willing to pay a few Euro more to get to/from the airport—it's a small portion of the overall cost of their trip—while the wide array of people taking public transit to work, school, recreation, etc... are more limited in their willingness and ability to pay. The rail system may desire to charge more to make as much as possible from these customers.
In some areas, the transit authority provides a special discount program (or alternate means of transportation with lower fares, such as bus service) for employees who work at the airport, so that they can charge higher fares to travelers willing to pay them while still serving commuters who travel to the airport daily.
To back up a second, "price discrimination" is not generally, in and of itself, illegal. Businesses discriminate by price all the time. Happy hour specials and lower matinee prices are examples of price discrimination. Businesses in wealthier areas may charge more. Transit systems sell the same service at higher prices for those buying single fares than those buying monthly passes and offer discounts to young people and seniors. Airlines sell seats at wildly different prices, sometimes charging less for passengers who stay over a Saturday night (likely more price-sensitive leisure travelers) and more for short-notice bookings (likely high willingness to pay business travelers) while unloading discounted inventory on opaque booking sites (likely price-sensitive leisure customers). Specific laws may govern the practice, such as those against racial or gender discrimination or public service requirements for things like public transit. But many businesses recognize that customers have different willingness to pay and try to price accordingly.