Yes, forming an alliance with Iran to beat back ISIS would leave Iran—which already has huge influence over the Iraqi government—stronger still. So, we have to decide which prospect we dislike less: an Islamist state in Iraq (perhaps joined with one in Syria) or a strengthened expansionary Iran.
This business of bad choices is nothing new. The most instructive precedent in recent times is the decision by Franklin D. Roosevelt and Winston Churchill to team up with Joseph Stalin for the sake of defeating Adolf Hitler. One outcome of this grand alliance was that, at the end of World War II, the Soviet Union controlled all of Eastern Europe. But the alternative would have been for Nazi Germany to control all of Europe, east and west, and perhaps eventually more.
Iraq is not Europe, ISIS is not the Nazis, Obama is not Roosevelt, Hassan Rouhani is not Churchill. In other words, the analogy is far from perfect. But the point is the same: Sometimes nations have to form alliances with unpleasant nations to prevent the victory of something worse.