Vienna and Budapest are only 240km apart and travel by train, bus, or car takes three hours or less. If you are looking for interesting places to visit on the way, here are a few suggestions.
The capital of Slovakia is the biggest city between Vienna and Budapest. It is much smaller than the two, but perhaps that is the very reason which makes it a good candidate for a short one or two day stopover. Places of interest include the Bratislava Castle, the iconic “UFO bridge” with a panoramic restaurant on top, and a (relatively small) old town centre. Sightseeing cruises on the Danube are also very popular.
Bratislava has fast and frequent train and bus connections to both Vienna and Budapest. Travel from Vienna to Bratislava takes about an hour, from Bratislava to Budapest two and half hours.
An alternative to Bratislava is Gyor, the sixth largest city in Hungary and the capital of the northwestern region that borders Austria and Slovakia. With population of 130,000, Gyor is much smaller than Bratislava and also less visited by tourists. That said, it has a lovely historical centre with numerous interesting building and plenty of shops. It may be a good place to take an afternoon stroll in between trains.
Gyor is directly on the main line, approximately halfway between Vienna and Budapest. Basically all trains stop here, as well as some (though not all) buses.
There is a large lake on the border between Austria and Hungary: Neusiedler See, a popular weekend or even afternoon destination for the Viennese, especially in summer when you can swim, sail, or windsurf here. It is also a famous birdwatching site.
The lake is surrounded by small towns and villages, the most popular ones being Neusiedl am See and Rust. The former is also the best way to access the lake, as it lies directly on the A4 motorway from Vienna to Budapest (it is km 43, exit Neusiedl/Parndorf).
You can also reach Neusiedler See by train from Vienna Hauptbahnhof in 40-50 minutes. After stopping in Neusiedl am See, the train goes all the way along the western coast, stopping in several other places on the lake.
To get from Neusiedler See to Budapest, you can take the same train back to Parndorf Ort (one stop after Neusiedl) and then another train to Hegyeshalom on the Hungarian side, where you can board an express train from Vienna to Budapest. The entire journey takes slightly above three hours.
By the way, if you like shopping, Parndorf has one of the largest outlet centres in Europe (Designer Outlet Parndorf). There are in fact shuttle buses from Vienna city centre (State Opera) to this shopping centre, and they can be used as an alternative way to get from Vienna to Neusiedler See (they are not free, but cheaper than trains).
The southern end of Neusiedler See belongs to Hungary. The Hungarian section of the coast is very short, but it is actually the nicest part. A natural reserve with several kilometres of wetlands separates the lake from Sopron, a historical city that is worth visiting on its own. The entire region is known for its wines, and there are numerous wine tasting opportunities in Sopron.
You can easily travel by train from Vienna to Budapest with a stop in Sopron. The trains depart from Hauptbahnhof and also stop at Wien Meidling station (near Schönbrunn Palace). Travel time to Sopron is 1:20. Some trains are direct, others with a change in Wiener Neustadt.
There are direct trains from Sopron to Budapest, taking two and half hours. They go usually in two hour intervals during the day, but note that there may be no more trains after 6-7pm. Sopron is in fact nice enough to stay overnight and have some wine in the evening. Moreover, accommodation can be cheaper than in Vienna and Budapest.