I would like vector variables to be bold faced, and scalar variables to be in plain font.
The components of vector variables should then follow the scalar (nonbold) convention.
To this end, using the embellishments of xparse I can, say, define a command \x as
\usepackage{bm} % for \bm command
\usepackage{xparse}
\NewDocumentCommand{\x}{e{^}}{%
\IfValueTF{#1}{x{#1}}{\bm x_{#1}}% plain if subscript provided, bold otherwise
\IfValueT{#2}{^{#2}}%
}%
that prints a plain "x" symbol if a subscript is provided or a bold "x" otherwise.
So far so good.
The problem arises when I want to combine this with a math accent command, such as \bar:

Is there any way to make the commands \x (and similarly defined \y, etc.) "compatible" with \bar and friends?
I tried with some \expandafter before \bar but no combination seemed to work.
Edit after egreg's solution
Following @egreg's idea of passing the accent as optional argument, I came with a simple modification of my original macro that also seems to do the trick. I don't know if it is TeXnically sound, but to make it more reader friendly I directly pass a possible accent command to a first optional argument.
\NewDocumentCommand{\x}{O{} t{'} e{_^}}{% #1: accent command #2: catch prime (') #3: subscript #4: superscript
% apply optional (accent) #1
% apply \vec if subscript #3 is empty
\IfValueTF{#3}{%
% check if subscript is provided but is empty
\ifstrempty{#3}{\vec{#1{x}}}{#1{x}_{#3}}%
}{%
\vec{#1{x}}%
}%
% add superscript
\IfValueT{#4}{^{#4}}%
% add prime (')
\IfBooleanT{#2}{'}%
}%
I also included a token argument to catch a possible prime ', so that it properly commutes with subscripts (\x_i' and \x_i' are treated equally).
Of course, it is not compatible with repeated primes (\x_i'' is correctly typed in plain font whereas \x''_i is printed in bold), but typing primes at the end works as expected.
In order to create similar commands, I wrapped the code into a \defineBoldVariableCommand{\commandName}{symbol}.
The MWE given below produces the following output:
\documentclass{article}
\usepackage{bm}% for bold math command \bm
\usepackage{etoolbox}% for \ifstrempty
\usepackage{xparse}% for \NewDocumentCommand
% ================================================================
\renewcommand{\vec}{\bm}% redefine \vec as bold math
\newcommand{\defineBoldVariableCommand}[2]{% #1: cmdname #2: symbol
\let#1\relax% overwrites definition, if command already defined
\NewDocumentCommand{#1}{O{} t{'} e{^}}{% ##1: accent command ##2: catch prime (') ##3: subscript ##4: superscript
% apply optional (accent) ##1
% apply \vec if subscript ##3 is empty
\IfValueTF{##3}{%
% check if subscript is provided but is empty
\ifstrempty{##3}{\vec{##1{#2}}}{##1{#2}{##3}}%
}{%
\vec{##1{#2}}%
}%
% add superscript
\IfValueT{##4}{^{##4}}%
% add prime (')
\IfBooleanT{##2}{'}%
}%
}%
\defineBoldVariableCommand{\x}{x}
% ================================================================
\begin{document}
No (or empty) subscript: variable is bold
\begin{tabular}{@{}ll@{}}
$\x = \x_{}$ & \verb|\x = \x_{}|
\\
$\x^k$ & \verb|\x^k|
\\
$\x[\bar]$ & \verb|\x[\bar]|
\\
$\x[\bar]^k$ & \verb|\x[\bar]^k|
\\
$\x'$ & \verb|\x'|
\\
$\x[\bar]'$ & \verb|\x[\bar]'|
\end{tabular}
\vspace{.5cm}%
Nonempty subscript: variable is in plain font
\begin{tabular}{@{}ll@{}}
$\x_i$ & \verb|\x_i|
\\
$\x_i^k = \x^k_i$ & \verb|\x_i^k = \x^k_i|
\\
$\x[\bar]_i$ & \verb|\x[\bar]_i|
\\
$\x[\bar]^k_i = \x[\bar]_i^k$ & \verb|\x[\bar]^k_i = \x[\bar]_i^k|
\\
$\x'_i = \x_i'$ & \verb|\x'_i = \x_i'|
\\
$\x[\bar]'_i = \x[\bar]_i'$ & \verb|\x[\bar]'_i = \x[\bar]_i'|
\end{tabular}
\end{document}
Edit #2. Simpler solution
Another possibility, that produces exactly the same outcome but with less work is given by suitably redefining \vec instead:
% ================================================================
\RenewDocumentCommand{\vec}{m O{} t{'} e{_^}}{%
#2{%
\IfValueTF{#4}{\ifstrempty{#4}{\bm}{}}{\bm}%
{#1}%
}%
\IfValueT{#4}{_{#4}}%
\IfValueT{#5}{^{#5}}%
\IfBooleanT{#3}{'}%
}
\newcommand{\x}{\vec{x}}
% ================================================================


\NewDocumentCommandproduces protected commands so you can't expand them. Generally I think that the idea behind your command is bad. Why don't you type simply x if you want a normal scalar x and e.g. \vec{x} if you want a vector? Instead you hide semantic differences behind some private syntax convention that only you can understand (and probably will have forgotten in a year) which means that nobody else will be able to properly understand your source. – Ulrike Fischer Oct 30 '23 at 08:41