Updated:

1. Create the counter

/scoreboard objectives add deaths deathCount "Deaths"
/scoreboard objectives setdisplay sidebar deaths

deathCount adds one each time a player dies. The sidebar sorts the most deaths to the top.

2. Tab list and below names

/scoreboard objectives setdisplay list deaths
/scoreboard objectives setdisplay below_name deaths

See the health display for the same slots with hearts.

3. Announce each death

/scoreboard objectives add died deathCount
# repeat, always active
execute as @a[scores={died=1..}] run tellraw @a [{"selector":"@s","color":"red"},{"text":" died! Total: "},{"score":{"name":"@s","objective":"deaths"}}]
# chain, always active
scoreboard players reset @a[scores={died=1..}] died

died is cleared after each message; deaths keeps the total. To share where they died, see the death location guide.

4. Three lives

execute as @a[scores={deaths=3..},gamemode=survival] run gamemode spectator @s

Run it in a repeat block. Pairs well with UHC.

5. Reset

/scoreboard players set @a deaths 0

6. Show lives left

/scoreboard objectives add lives dummy "Lives"
# repeat, always active
scoreboard players set @a lives 3
# chain, always active
execute as @a run scoreboard players operation @s lives -= @s deaths
# chain, always active
execute as @a run title @s actionbar [{"text":"Lives left: "},{"score":{"name":"@s","objective":"lives"}}]

Each tick sets lives to 3 and subtracts deaths, then shows the result on the action bar. With the three-lives rule above, players see how close they are to elimination. For five lives, change both the 3 here and the 3.. in step 4.

Troubleshooting

Check your command

The Command Error Doctor checks common syntax mistakes in your browser.