I asked the same question a couple months ago, maybe. Most people said "yeah, you can leverage the loan and stay invested." One person said "no no no, leverage risk!" You aren't missing anything here, if the opportunity cost is greater than the interest rate, yes, you come out ahead. But 7% returns are not guaranteed, however, so you don't know if you will actually come out ahead when looking at a 5 year term. Chances are you will, but we don't know that for sure. You know you will definitely lose money paying interest, though.
I don't recall if I threw a dollar amount out there as an example, but my answer at the end of all this is pay cash for car that doesn't cost a lot to minimize opportunity cost.
Also, your tax estimate is overstated. You wouldn't pay tax on the entire value of the investment after the 5 years, just the gains. And also, if you are lucky to have a low enough income like me (ha ha) your capital gains tax rate will be zero, anyway.