Much of the cost of even a print book is in fixed factors: authorship, editing, proofreading, marketing, cover design, and the like. These may vary by title and it's quite possible to skimp on elements, but for a highly-produced work (references, fact-checking, illustrations, data tables, etc.) can be significant. Those costs are amortised over the print run and sales, which for most books is quite small, perhaps a few thousands of copies.
Digital publishing bypasses print, distribution, warehousing, and materials, but still includes those fixed costs.
Keep in mind that many publishers effectively act a bit like investors or VC, seeding a large number of publications in the hopes that a small number succeed. The name "Random House" was said by its founding publisher, Bennet Cerf to be "We just said we were going to publish a few books on the side at random". As in music and cinema, a few "tentpole" projects support much of the rest of the enterprise.
<https://en.wikipedia.org/wiki/Random_House#Company_history>.