Here's a way to do it with Lyx.
As far as I can see both from checking out Lyx 2.3.7, and internet search, there are no GUI-action prepared for what you want to do. I tried two things.
First, which is close to what you want to do by mouse:
- M=
- Alt + M (
- insert Matrix 3*3
- which creates the first row
- as you can see, it wraps an array environment into math mode ($ .. $)

Second:
There was a similar question, which was answered in Latex-terms. Actions:
- insert TEX code (crtl + L)
- put math mode $$ or [ ] depending on your goal
- copy said code (or write it down here by hand) into the red frame
What's missing is specifying the package:
- Document/Settings
- LaTeX Preamble
- enter the package call from said answer

From watching the LaTeX (pdflatex) preview window I know, I copied as it should be.
Compiling (ctrl + r, and you're done) I have to do in a different way, as my setup is still quite immature:
- save
- File/Export/Latex (pdflatex)
- open the new .tex file in my current Latex environment (Texmaker)
- compile
There I am:

Lyx-file:
This is how Lyx stores results from GUI-actions; past this code into a file, say yours.lyx, and open it with Lyx to see my edits:
#LyX 2.3 created this file. For more info see http://www.lyx.org/
\lyxformat 544
\begin_document
\begin_header
\save_transient_properties true
\origin unavailable
\textclass article
\begin_preamble
\usepackage[renew-dots,renew-matrix]{nicematrix}
\end_preamble
\use_default_options true
\maintain_unincluded_children false
\language english
\language_package default
\inputencoding auto
\fontencoding global
\font_roman "default" "default"
\font_sans "default" "default"
\font_typewriter "default" "default"
\font_math "auto" "auto"
\font_default_family default
\use_non_tex_fonts false
\font_sc false
\font_osf false
\font_sf_scale 100 100
\font_tt_scale 100 100
\use_microtype false
\use_dash_ligatures true
\graphics default
\default_output_format default
\output_sync 0
\bibtex_command default
\index_command default
\paperfontsize default
\spacing single
\use_hyperref false
\papersize default
\use_geometry false
\use_package amsmath 1
\use_package amssymb 1
\use_package cancel 1
\use_package esint 1
\use_package mathdots 1
\use_package mathtools 1
\use_package mhchem 1
\use_package stackrel 1
\use_package stmaryrd 1
\use_package undertilde 1
\cite_engine basic
\cite_engine_type default
\biblio_style plain
\use_bibtopic false
\use_indices false
\paperorientation portrait
\suppress_date false
\justification true
\use_refstyle 1
\use_minted 0
\index Index
\shortcut idx
\color #008000
\end_index
\secnumdepth 3
\tocdepth 3
\paragraph_separation indent
\paragraph_indentation default
\is_math_indent 0
\math_numbering_side default
\quotes_style english
\dynamic_quotes 0
\papercolumns 1
\papersides 1
\paperpagestyle default
\tracking_changes false
\output_changes false
\html_math_output 0
\html_css_as_file 0
\html_be_strict false
\end_header
\begin_body
\begin_layout Standard
M=
\begin_inset Formula $\left(\begin{array}{ccc}
& a & b\
c & 1 & 2\
d & 3 & 4
\end{array}\right)$
\end_inset
\end_layout
\begin_layout Standard
\begin_inset ERT
status open
\begin_layout Plain Layout
$M=
\backslash
begin{pmatrix}[first-row,first-col]
\end_layout
\begin_layout Plain Layout
& a & b
\backslash
\backslash
\end_layout
\begin_layout Plain Layout
x & 1 & 2
\backslash
\backslash
\end_layout
\begin_layout Plain Layout
y & 3 & 4
\end_layout
\begin_layout Plain Layout
\backslash
end{pmatrix}$
\end_layout
\end_inset
\end_layout
\end_body
\end_document
Compared to Texmaker or any other Latex setup
All I then need to do is:
- creating a new file
- copy code from said answer (or type it manually)
- save
- compile
Time and again, GUIs seem to be neat, but add a lot of new complexity, often paired with little flexibility ... while knowing Latex by heart, having a nice supporting editor and some command line based compille will be fast, less error prone, keep you going.

nicematrixpackage allows you to add exterior rows and columns – bonk Jan 22 '24 at 14:54