If your unit cost is $50 and your unit revenue is $100, your unit profit is $50. If you sell 100,000 units, your business revenue is is $10M. If you spend $10M on R&D (and other overhead), your business costs are $15M and your business profit is -$5M.
BUT, if you sell 1M units, your revenue is $100M, your costs are $60M ($50M unit + $10M R&D), and your profit is $40M.
That's the power of scale.
(Compare this to a business with a negative unit profit: it won't scale no matter what you do.)
$100M revenue - ($35M + $10M) ==> $55M profit.
Tesla would lose money per car if, for example, the raw materials cost more money than they were selling it for (this is not the case)