Passing optional arguments to \printbibliography as variables

I propose a much more flexible approach:

\begin{filecontents}{\jobname.bib}
    @article{article1,
      title={Title 1},
      author={Author1},
      journal={Journal1},
      year={2019},
      keywords={Red}
     }
   @article{article2,
     title={Title 2},
     author={Author2},
     journal={Journal 2},
     year={2019},
     keywords={Blue}
     }
\end{filecontents}

\documentclass[a4paper,10pt]{article}
\usepackage{xparse}
\usepackage[backend=biber]{biblatex}

\addbibresource{\jobname.bib}

\ExplSyntaxOn
\NewDocumentCommand{\xforeach}{sO{##1}mm}
 {
  \cs_set:Npn \__ross_xforeach:w #2 \q_stop { #4 }
  \IfBooleanTF { #1 }
   {
    \clist_map_inline:on { #3 } { \__ross_xforeach:w ##1 \q_stop }
   }
   {
    \clist_map_inline:nn { #3 } { \__ross_xforeach:w ##1 \q_stop }
   }
 }
\cs_generate_variant:Nn \clist_map_inline:nn { o }
\ExplSyntaxOff

% Setup a list of keywords
\newcommand*{\myKeywords}{Red,Blue}

\begin{document}
\nocite{*}

\printbibliography

\printbibliography[title=Red,keyword=Red]

\printbibliography[title=Blue,keyword=Blue]

% How to pass \keyword to \printbibliography as an optional argument?

\xforeach{Red,Blue}{\printbibliography[title=#1,keyword=#1]}

\xforeach*{\myKeywords}{\printbibliography[title=#1,keyword=#1]}

\xforeach[#1/#2]{
  This is the red bib/Red,
  This is the blue bib/Blue
}{\printbibliography[title=#1,keyword=#2]}

\end{document}

The \xforeach command has

  1. a *-variant (meaning the list is given implicitly, such as with \myKeywords)
  2. an optional “item template” argument (default #1) that tells how to split into parts each item (in the final example, #1/#2)
  3. the code to be executed on each item, with #1 to denote the current item (or with further placeholders if the optional argument is used).

enter image description here


same technique as in https://tex.stackexchange.com/a/228333/190358 also works here:

\documentclass[a4paper,10pt]{article}
%
\usepackage{filecontents}
\begin{filecontents}{\jobname.bib}
    @article{article1,
      title={Title 1},
      author={Author1},
      journal={Journal1},
      year={2019},
      keywords={Red}
     }
   @article{article2,
     title={Title 2},
     author={Author2},
     journal={Journal 2},
     year={2019},
     keywords={Blue}
     }
\end{filecontents}
%
\usepackage[backend=biber]{biblatex}
\addbibresource{\jobname.bib}
\usepackage{tikz}
% Setup a list of keywords
\newcommand*{\myKeywords}{Red,Blue}
\begin{document}
\nocite{*}
%\printbibliography
%\printbibliography[title=Red,keyword=Red]
%\printbibliography[title=Blue,keyword=Blue]
% How to pass \keyword to \printbibliography as an optional argument?
\foreach \keyword in \myKeywords {
\begingroup\edef\x{\endgroup\noexpand
    \printbibliography[keyword=\keyword]
}\x    
}
\end{document}