Note: this is a question about railway - mounted artillery, NOT about armored trains (which were more akin to tanks-on-rails and had much smaller guns)
The idea behind railway gun is quite sound: a bigger gun is better than a small gun but it is harder to relocate. But if we will use trains, that can carry lots of weight relatively quickly, then we can put REALLY BIG guns on them and move them around with ease!
But the effect is far from expected because the railway artillery requires LOTS of maintenance and logistics, might have problems with aiming in other direction than straight ahead of the tracks and the number of shots per day is simply pathetic:
For example, the famous Dora (no, not that one) required 250 people to assemble the gun in 3 days, 2500 to lay the tracks and 2 flak battalions to protect it from the air attacks and in return, it was providing 14 shots per day.
All this looks horribly inefficient when compared to for example WW II bombers or even smaller, WW I heavy artillery, like the famous Big Bertha
(which could fire 8 shots per hour, although indeed with ~7 times lighter shell).
So, were ever those "white elephants" of modern weapons even critical in winning any battle? Or were there just a drain on the resource, that could be better spent on a different kind of weaponry?
I've removed the "winning" part since indeed it had not much sense - U-boats were critical in the battle of Atlantic, yet the battle was won by Allies. So in other words, I am looking for battles where railway guns have played a very significant role, no matter on the final outcome.
The only time they were used - although on a different front - they did more or less what they were intended to do.
The German leadership was well aware about the logistical nightmare these guns imposed on the army. The fact that only two were ever ordered shows the over all value even the Germans assigned to these guns. Yet they were deemed necessary to break an eventual stalemate.
The super heavy guns like the 80cm "Schwerer Gustav" were purpose built guns to defeat the bunkers on the Maginot line. They were a weapon developed and built for a single purpose - that never came.
The Germans never thought they would rush through France like they did during the Blitz and were rather planning for a prolonged siege like during WW I.
Since they marched through France in mere 6 weeks they were now sitting on now practically obsolete ordnance not knowing what to do with it. When, later on, the German offensive in Russia faced the fortifications of Sevastopol, they actually found a use for the guns they've been sitting on for almost two years.
Sevastopol - on the very edge of the Crimea peninsula - was a major Russian stronghold and hard to conquer since the fortification there were enormous. In the end, not even the large 80cm gun alone could cripple the fortress. A combined effort of the 42cm, 60cm and 80cm guns was needed to knock out the Russian artillery and crippled the fort.
Since the siege itself took more than 9 months to conclude, there was quite enough time to transport and assemble the railway gun there. These super heavy artillery pieces did play a role in defeating the defenses and freed up resources needed on the rest of the eastern front. So while no single piece of equipment can "win" a siege on itself, these guns did contribute - although mainly because the army found an opportunity to use the otherwise useless guns and not because they were such superior equipment.
No, the development of the railway gun came too late. Airplanes could do much the same, far more efficient, and at less cost.
As you said yourself, railway artillery needed LOTS of maintenance. 14 shots per day is not exactly very efficient. Do mind that after 18-20 shots, the barrel was worn out and needed replacement. Before that, each shot would change (widen) the bore diameter. Which required each shot had to be recalculated and adjusted to compensate for that. That (barrel changes) happens with other guns too, but after 500-2000 shots, which is a big difference.
During WW1, there was an excuse: airplanes weren't yet capable of delivering the same amount of bombs on one particular spot. This wasn't the case during WW2. If you needed a lot of ordnance on a particular target, simply use more bombers. Need more impact? Use bigger bombs.
WW1 railway guns weren't really efficient for the investment, but it worked. During WW2 it was a sheer waste of money, time and effort. That's why the allies didn't invest in it. The British used railway guns for coastal defense, but those guns were left overs from WW1 battleships.
The Germans used railway guns to repel the landings at Anzio with moderate success, and the Karl Gerät with more success during the siege of Sebastopol. Looking back, the investments weren't worth it. The few times a railway gun actually made a difference, it could have been done with different weapons are far less cost and effort.
Railway guns were outdated by WW2, But it's complicated. They were designed to be artillery, bunker-buster and defensive. Though the German examples during WW2 were the most notable, the British, French, American, Italian... Even Polish and Russian examples had niches. For example, Gladiator (British) was used to train Naval crews on the guns without having any serviceable ships taken out for training, and in that role it meant that British and American crews had better experience using naval guns as large field artillery, which was useful during D-day.
Offensively, they didn't see much use, but some of the Railway Operating Division staff (again, British) noted their Italian counterparts had been using them decisively for port defence, and a captured Italian gun was used against a sea fort by the Australians. (Railways at War, 1982? I think).
As a design, railway guns had been used decisively in wars before WW1. But they were fortress-breakers, for the Sigfried and Maginot lines. Aeroplanes and railway warfare and sabotage made their greatest strength, Mobility, in to a weakness. the British guns, for the most part, were anti-ship and training, and as they pointed out, in that role, were more of a liability than Whinnie, the dug-in naval gun. They notably "lost" Boche-Buster for a short period because they fly-shunted it (took all the brakes off and pushed it around with a loco) only to discover they couldn't stop the juggernaut. And in many ways, in theory they had a niche through WW2, but the sort of offensives they did best in, didn't happen during WW2. Sevastapol is probably the last great hurrah of the Railway Gun in an offensive role. They were for doing a high-velocity surgical job against things no other gun could hit. Some of the last Port Defence examples lasted a bit longer (fairly sure some remained around the Panama and Suez Canal entrances for a few decades) but even in that role, a lot of that style of gun had been replaced by torpedo stations. Some Smaller railway artillery setups built by Vickers overcame a lot of the problems of the railway gun, but came in too late for any actual, practical use. rotating platforms so that a line of artillery could be pulled in to position, a middle ground between the wartrain and the railway gun, worked but sat in a niche that was basically filled by the same thing on tank chassis, or bombers.
(again, Railways at War) It should perhaps be observed that, particularly with the Italian ones, they were considered terrifying enough to merit the retreating Axis to make every effort to stop their possible advances towards the German heartlands. After the Italian examples were captured, the Germans basically doubled track-removal efforts, and it's notable that at several Italian campaign locations, the ROD were requested to at least try to get them in to dislodge forces or push for a surrender. The Germans literally completely removed one railway (not just breaking the ties or sleepers, but physically removing every last bit of rail) because it was the only line where they knew a gun would have to pass. Their problem was, they were too slow to do so and were captured by the British ROD who were relaying! The Italian campaign increasingly fell back on defensive fortress lines, the ideal situation for the railway gun, particularly as they were (mostly) out of bombing range, but none were negotiated up from the south.
One final problem noted by the Brits (and they observed in the French and Americans too.) was that the railway gun programme was operated by the Royal Naval Air Service, Railway Operating Division, Army Marines and Navy, and had RAF spotters too. Tactically and logistically, it was a case of Too Many Cooks, particularly as these different forces held and operated different rank systems and had different considerations on what the gun should be used for, and where. Add in that a lot of the RNAS's railway section was handed to the Poles, and it became a logistical nightmare in to which people intentionally went to avoid being sent to the front. I'm not sure of the German arrangements, But it mostly seems similar.