Well I guess we could think about this in the way of if Gandalf had let say died at the battle of the Pelennor Fields or at Helm's Deep would he have come back?
Well I would say yes. Because as others have said he was working towards the task (defeating Sauron), and it just happened that he got "delayed" in a way. But if he would have not been working towards the task, he probably would have not been sent back.
Like if it would have been a thousand years earlier, and lets say he tripped and fell into a river and drowned. I doubt that he would have been reincarnated for that reason