I think it's the right thing to do, of course. What motivated the statement at this point in time could be worth pondering over.
My guess is that Obama needs the youth vote and something of this sort is needed to get the young voters energized. At the same time, those who are firmly opposed to same-sex marriage would most likely not vote for Obama in the first place, because of the other opinions which are bundled with that opposition.
Still, all this is pure guessing.