Everyone has given good answers, and corrected your misunderstandings, but I want to put it all in one response to make it clear.
1. Stocks earn money in 2 ways:
a: dividends, which are payments made to you as a shareholder
b: capital gains, which is when you sell the stock for more than you paid for it. Only the amount of growth is income, and it is not realized as income until it is sold.
Overall, considering both sources, stocks grow/make on average 10-11% per year (with a whole lot of variability). Dividends usually make up a smaller portion than capital gains.
2. Earned income is a specific definition, meaning income from a job that FICA taxes are paid on. Dividends and capital gains are considered income, but not earned income. The withdrawal from a taxable investment account will be a combination of basis (amount paid initially) and growth (capital gains). Only the capital gains will be considered income. So in your example, you would have $9000 of dividend income, and some portion of the $31,000 would be capital gains.
The other point is that spending money and realizing income are completely different. You could withdraw $40,000 for spending, and the realized income on that amount would be much less. Alternately, you could sell hundreds of thousands of dollars in order to realize $40,000 of income, and then reinvest all of it, spending nothing.
3. ACA subsidies are not based on earned income. They are based on MAGI, which is all sources of income minus specific deductions. If living off of investments, you can control your income by selling enough investments to realize the amount of income you want. You may run into trouble if your investments do not grow enough, or lose value. Then you may not have enough capital gains in order to realize the income level that you want.
A lot of early retirees realize income by making Roth conversions. This method is more stable than relying solely on capital gains. If you have money in an IRA or a 401k, you can use this method. You simply move the amount of income you want to realize from the traditional IRA to the Roth IRA, and that will show up as income on your taxes.
For example, let's say that you have $500,000 in a taxable account, and $500,000 in retirement accounts. You want to spend $40,000 this year, and want to have at least $28,000 in income in order to stay off of medicaid. You get $9,000 in dividends, and withdraw $31,000 out of your taxable account to make up the rest of the $40,000. Let's say that only $6,000 of that $31,000 is capital gains. So you have your $40,000 of spending money, but only $15,000 of income. You need $13,000 more in income, but don't need more spending money. You can either:
a: sell more from your taxable account, and then reinvest it in order to realize an additional $13,000 of capital gains, or,
b: move $13,000 from your traditional IRA to a Roth IRA in order to realize $13,000 of additional income.
Extra info on ACA subsidies for those not in your exact situation:
There are 3 subsidy cliffs to watch out for.
1. 400% FPL: go over this, and you lose all subsidies
2. 250% FPL: go over this, and you lose the cost-sharing subsidies, which pay a portion of your deductible if you get a silver plan. You still get the subsidy on your monthly premium.
3. The medicaid cliff.
a. if you are in a state that expanded medicaid, 138% FPL is the cut-off. Go UNDER that, and you will be covered by medicaid. In some places, it's hard to find physicians who take medicaid, but in other places, medicaid is an excellent plan.
b. if you are in a state that did NOT expand medicaid, 100% FPL is the cut-off. Go UNDER that, and you lose all subsidies for ACA, and probably won't qualify for medicaid at all, as there is usually some combination of work requirements, income limits, and asset limits.