In the chapter "Many Partings" the hobbits and Gandalf meet Saruman and Grima on the road. Saruman's intentions are pretty obvious; he even threatens the party:
Well, it will serve you right when you come home, if you find things less good in the Southfarthing than you would like. Long may your land be short of leaf!’
Gandalf clearly knows what is up:
I fancy he could do some mischief still in a small mean way
Saruman is a Maia and still has powers beyond mortals (his voice). It seems out of character that Gandalf did not intervene to stop another Maia, even without using his magical powers or at least warning the Hobbits so that they could take him prisoner and, conveniently, take him to Rivendell where they were going anyway.
There are many literary reasons of why the events leading up to the Scouring of the Shire are there in the story but I struggle to find an in-universe reason of why Gandalf did not try to stop Saruman.
Edit: while there are many reasons Gandalf would not have intervened directly, I have not seen a satisfactory reason of why he could not have at least urged the Hobbits to go directly to The Shire instead of going to Rivendell.