Capitalize titles of persons when used as part of a proper name but usually not when used alone - Capitalization - Mechanics

Rules for writers, Tenth edition - Diana Hacker, Nancy Sommers 2021

Capitalize titles of persons when used as part of a proper name but usually not when used alone
Capitalization
Mechanics

District Attorney Marshall was reprimanded for badgering the witness.

The district attorney was elected for a two-year term.

Usage varies when the title of an important public figure is used alone: The president [or President] vetoed the bill.