Contra the other answers, the national rail ticket machines will sell a ticket from Leuchars to Edinburgh Airport that allows using the tram. When you are controlled, on the tram, you get a free transfer tram ticket.
For the return trip, there’s a train ticket machine near the M & S at EDI.
If this is a regular daily journey can be done with a single ticket for the train and buses, unfortunately it doesn’t include the Edinburgh trams. It’s a rail+bus OneTicket which is a zonal season ticket available in weekly, monthly or annual versions.
It is also possible to buy a train ticket to “St Andrews Bus” which gives you a train journey to Leuchers and a bus journey to St Andrews. You can then buy a plus bus add on ticket for within Edinburgh including the tram (excluding the airport). However these would need to be done before getting to the station, e.g. the day before.
No, it’s not possible to do it on a single ticket as each of the operators is a different entity.
Alternatively you could take a Stagecoach bus 747 from Edinburgh Airport to Halbeath Park & Ride in Fife then change to a Stagecoach X24 bus to St Andrews bus station, which as Willeke suggested can be booked on a single ticket (a “Fife dayrider Plus”).
Travelinescotland is a useful site for planning journeys.
Credit:stackoverflow.com‘