First, the visit to Hiroshima was part of a state trip for the G7 conference in Japan - so it's not like he made a special trip to Japan to do it.
Second, sometimes diplomacy means doing things that impact the people where you are - not just the people at home. And yeah, being nuked is still a bit of a sore point in Japan (one of a dwindling number of good allies on that side of the Pacific), and there is ongoing tensions - especially in Okinawa - about US military presence there. So maybe playing nice on this is part of a larger picture. Most things are. Do you want to wind up with the only major bases in Asia to be in Korea if local anti- US military sentiment gets too strong?
Third, the stated reason was that Obama intends to "highlight his continued commitment to pursuing the peace and security of a world without nuclear weapons,"
Take that as you wish.
And it is an opportunity to make a certain type of speech as your career winds down. And being able to send a message while you're the lame duck may have been part of it too. Especially with renewed muscle-flexing from Putin, and if there is any lingering notions in Iran to restart any R&D.
Lots of reasons I can think that may have made it seem like a good idea, and it seemed to be properly received on all fronts.