First, remember that a company is owned by its investors - it is not owned by 'itself'. It is true that a company might choose not to always pay dividends, but that is a decision which should benefit the owners not the company itself.
So while dividends are not mandatory, the owners of the company have an opportunity cost they suffer by continuing to own shares, when instead they could liquidate the company [in theory] and put funds elsewhere. More realistically, if a company doesn't earn enough to compensate owners for the risk of ownership, they will sell to others, for lower and lower prices, until new owners are satisfied that their [now smaller] investment amount is appropriately compensated company earnings.
So when a company views the profitability of a new project, it should consider whether it will (a) pay an appropriate portion of the company's overall debt servicing costs [interest]; AND (b) pay an appropriate portion of the return required on equity given the assessed risk level of the business. If a new project earns less than (a) + (b), then it is theoretically better from a corporate finance perspective to just pay out the investment amount as a dividend instead.
Note that the cost of equity is not purely the amount of dividends paid - even non-dividend paying companies consider a cost of equity, which again reflects the opportunity cost taken by owners of the company to hold shares. There are many levels of attempted 'accuracy' at assessing what the cost of equity is for a business, and in some cases it may be taken as a simple rule of thumb for that industry - something like 'if debt cost is 3%, we should anticipate equity cost to be 10%'. That rate is then used to assess whether new projects actually 'pay for themselves', in the eyes of shareholders.